﻿#include "Generation/Piecemeal/Rect.h"

TArray<FVec> FRect::Trace(int direction)
{
	TArray<FVec> result;
	// 不止一行一列
	if ((width() > 1) && (height() > 1))
	{
		// TODO(bob): Implement an iterator class here if building the list is slow.

		// Trace all four sides.
		// 勾勒四边
		switch (direction)
		{
		case -1: //全部方向
			for (int x = left(); x < right(); x++)
			{
				result.Add(FVec(x, top()));
				result.Add(FVec(x, bottom() - 1));
			}

			for (int y = top() + 1; y < bottom() - 1; y++)
			{
				result.Add(FVec(left(), y));
				result.Add(FVec(right() - 1, y));
			}
			break;
		case 0: //top()
			for (int x = left() + 1; x < right() - 1; x++)
			{
				result.Add(FVec(x, top()));
			}
			break;
		case 1: //bottom()
			for (int x = left() + 1; x < right() - 1; x++)
			{
				result.Add(FVec(x, bottom() - 1));
			}
			break;
		case 2: //left()
			for (int y = top() + 1; y < bottom() - 1; y++)
			{
				result.Add(FVec(left(), y));
			}
			break;
		case 3: //right()
			for (int y = top() + 1; y < bottom() - 1; y++)
			{
				result.Add(FVec(right() - 1, y));
			}
			break;
		}
	}
	// 一行
	else if ((width() > 1) && (height() == 1))
	{
		result = TArray<FVec>();
		// A single row.
		for (int x = left(); x < right(); x++)
		{
			result.Add(FVec(x, top()));
		}
	}
	// 一列
	else if ((height() >= 1) && (width() == 1))
	{
		result = TArray<FVec>();
		// A single column, or one unit
		for (int y = top() + 1; y < bottom() - 1; y++)
		{
			result.Add(FVec(left(), y));
		}
	}

	// Otherwise, the rect doesn't have a positive size, so there's nothing to
	// trace.
	return result;
}

FRect::FRect(int x, int y, int width, int height)
{
	pos = FVec(x, y);
	size = FVec(width, height);
}
